<?php
require_once("../utility.php");

abstract class tableDescriptor{
	protected $columns;
}

abstract class db{
	protected $host;
	protected $dbName;
	protected $user;
	protected $password;

	public function __construct($_host, $_name, $_user, $_password){
		$this->host     = $_host;
		$this->name     = $_name;
		$this->user     = $_user;
		$this->password = $_password;
		$this->Connect();
	}
	abstract protected function Connect();
	
	abstract protected function query($qString, $getNRows = false);
	abstract public function fetchArray();
	
	abstract public function TableExists($tableName);
	abstract public function TableCreate($tableName, $dataArray);
	abstract public function TableCreateMultipleKey($tableName, $dataArray, $autoincrementKey);
	
	abstract public function AddRecord($tableName, $columns, $record);
	abstract public function GetRecord($tableName, $keyName, $value);
	
	abstract public function StartRead($tableName, $keyName, $fromValue, $toValue);
	abstract public function GetNextRecord();
	abstract public function LastRecord($tableName, $key1, $key2 = NULL);
	
	abstract protected function Disconnect();
}


class dbMySQL extends db{
	private $active;
	private $obj;
	private $result;

	public function __construct($_host, $_name, $_user, $_password){
		$this->active	= false;
		$this->result	= false;
		parent::__construct($_host, $_name, $_user, $_password);
	}

	protected function Connect(){
		if(!$this->active){
			if($this->obj = new mysqli($this->host, $this->user, $this->password, $this->name)){
				if(mysqli_connect_errno()){
					echo "Errore in connessione al DB: ".mysqli_connect_error();
					exit();
				}
				else
					$this->active = true;
			}
		}

		return $this->active;
	}

	protected function query($qString, $getNRows = false){
//echo("<br>MYSQL&gt; $qString... ");
		$this->result = $this->obj->query($qString);
/*echo "<pre>";
var_dump($this->result);
echo "</pre>";*/
		if($getNRows)
			return $this->result->num_rows;
	}

	public function fetchArray(){
		if($this->active && $this->result && $this->result->num_rows){
			return $this->result->fetch_array(MYSQLI_ASSOC);
		}

		return false;
	}
	
	public function TableExists($tableName){
		if($this->query("SHOW TABLES LIKE '".$tableName."'", true)==1)
			return true;
		
		return false;
	}
	
	public function TableCreate($tableName, $dataArray){
		if($this->active){
			$qString = "CREATE TABLE $tableName(";
			
			$primaryKey = NULL;
			foreach ($dataArray as $colNameType){
				if(empty($primaryKey))
					$primaryKey = $colNameType["name"]; 
						
				$qString .= $colNameType["name"]." ".$colNameType["type"]." NOT NULL,";
			}
			$qString .= " PRIMARY KEY ($primaryKey) );";

			$this->query($qString);
		}		
	}
	
	public function TableCreateMultipleKey($tableName, $dataArray, $autoincrementKey){
		if($this->active){
			$qString = "CREATE TABLE $tableName(";
			
			$primaryKey = NULL;
			foreach ($dataArray as $colNameType){
				if(empty($primaryKey))
					$primaryKey = $colNameType["name"]; 
					
				$qString .= $colNameType["name"]." ".$colNameType["type"]." NOT NULL, ";
			}
			if(isset($autoincrementKey)){
				$qString .= "$autoincrementKey INT NOT NULL AUTO_INCREMENT, ";
				$qString .= "PRIMARY KEY ($primaryKey, $autoincrementKey) ) ";
			}
			else
				$qString .= "PRIMARY KEY ($primaryKey) )";
			
			$qString .= "ENGINE = MyISAM;";

			$this->query($qString);
		}		
	}

	public function AddRecord($tableName, $columnsName, $record){
		if($this->active){
			$qString = "INSERT INTO ".$tableName." (".implode(",", $columnsName).") VALUES (".implode(",", $record).")";
			$this->query($qString);

			return true;
		}

		return false;
	}
	
	public function GetRecord($tableName, $keyName, $value){
		if($this->active){
			$qString = "SELECT * FROM ".$tableName." WHERE ".$keyName."=".$value;
			$this->query($qString);
			return $this->fetchArray();
		}

		return NULL;
	}

	public function StartRead($tableName, $keyName, $fromValue, $toValue){
		if($this->active){
			$qString = "SELECT * FROM ".$tableName." WHERE ".$keyName." BETWEEN ".$fromValue." AND ".$toValue;
			$this->query($qString);
		}
	}
	
	public function GetNextRecord(){
		if($this->active && $this->result){
			return $this->fetchArray();
		}
		return NULL;
	}
	
	public function LastRecord($tableName, $key1, $key2 = NULL){
		if($this->active){
			$qString = "SELECT * FROM $tableName ORDER BY $key1 DESC";
			if(isset($key2))
				$qString .= ", $key2 DESC";
			$qString .= " LIMIT 0, 1";
			
			$nRows = $this->query($qString, true);
			if($this->result && $nRows){
				return $this->fetchArray();
			}
			return NULL;
		}
	}
	
	public function Disconnect(){
		if($this->active){
			$this->obj->free();
			$this->obj->close();
			$this->active = 0;
		}
	}
}

?>
